package com.batch.job;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;


// 主要学习熟悉Flow的概念(学习章节1.2)
// 学习当前Job，请将@SpringBootApplication 取消注释，并将其他Job的@SpringBootApplication 方法注释,否则会对当前结果产生干扰

//@SpringBootApplication
// 因为用的Step路径在别的目录下，因此需要手动加ComponentScan
@ComponentScan(basePackages = "com.batch")
// EnableBatchProcessing 使spring创建springbatch操作相关类对象
@EnableBatchProcessing
public class HelloJob2 {

    @Autowired
    private ApplicationContext context;

    // Job构建工厂，用于构建Job对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Bean
    public Flow flow02_1(){
        return new FlowBuilder<Flow>("flow02_1").start((Step) context.getBean("step01"))
                .on("COMPLETED").to((Step) context.getBean("step02")).end();
    }

    @Bean
    public Job job02_1(){
        return jobBuilderFactory.get("hello-job02-02")
                .start(flow02_1()).end().build();
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloJob2.class,args);
    }

}
